import re
import requests
from bs4 import BeautifulSoup
import os
from selenium import webdriver
import queue
import time
from selenium.webdriver.chrome.options import Options


def get(url):
    ch_options= Options()
    ch_options.add_argument('--headless')
    try:
        #driver = webdriver.Chrome(executable_path="D:\\pythonglr\python\\driver\\chromedriver",chrome_options= ch_options)
        driver = webdriver.Chrome(executable_path="D:\\pythonglr\python\\driver\\chromedriver")
        driver.get(url)
        html= driver.page_source
        #time.sleep(120)
        driver.close

        return html
    except Exception as e:
        print(1,3)
        return ''


index_url = "https://app.finance.ifeng.com/list/stock.php?t=hs"
url_dict= {}
index_pagesource= get(index_url)
soup= BeautifulSoup(index_pagesource,'html.parser')

table= soup.table
#print(1,table)
trs= table.find_all('tr')
#print(1,trs)
for tr in trs:
    stock_code= tr.find('a').string#找到tr中的第一个标签
    #print(1,stock_code)
    if 'href' in tr.find('a').attrs.keys():
        stock_url= tr.find('a').attrs['href']
        #print(1,stock_code,stock_url)
    if re.search(r'^\d+$',stock_code):
        url_dict[stock_code]= stock_url
print(url_dict)


